package com.helei.web.controller;


import com.google.gson.Gson;
import com.helei.common.api.Result;
import com.helei.security.component.JwtUtil;
import com.helei.security.component.UserAuthentication;
import com.helei.security.service.MyUserDetailsService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * @author hl
 * @date 2021-07-14 00:19:58
 */
@RestController
@RequestMapping("test")
public class AppUserController {
    @Resource
    MyUserDetailsService myUserDetailsService;

    @PostMapping("/a")
    public Object index() {
        return"security jwt";
    }

    @PostMapping("/login")
    public Object login(String name, String password) {
        Assert.notNull(name, "账号不能为空");
        Assert.notNull(password, "密码不能为空");
        UserAuthentication userDetails = (UserAuthentication) myUserDetailsService.loadUserByUsername(name);
        if (!new BCryptPasswordEncoder().matches(password, userDetails.getPassword())) {
            return Result.ok();
        }
        String token = JwtUtil.generateToken(userDetails.getExtendUserBean(), userDetails.getUsername(), userDetails.getRoles());
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("menu",userDetails.getMenu());
        return map;
    }

    public static void main(String[] args) {
        System.out.println(new BCryptPasswordEncoder().encode("123"));
        System.out.println(new BCryptPasswordEncoder().matches("123", "$2a$10$1epp7UWNkM9np7QjfJgaPenzshslauiHBgN4AkaJkvwNlHXpFSi1O"));
        System.out.println(UUID.randomUUID());
    }

    @PostMapping("/role")
    //@PreAuthorize("hasAnyAuthority('ROLE_a')")
    public String roleInfo(int a) {
        Assert.isTrue(a==1,"dsdsdsadas");
        return "需要获得ROLE_a权限，才可以访问";
    }

    // @PreAuthorize("hasAnyAuthority('kdream')")
    @PostMapping("/roles")
    @Secured("ROLE_bb")
    public String rolekdream() {
        UserAuthentication userDetails = (UserAuthentication) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(new Gson().toJson(userDetails));
        System.out.println(userDetails.getExtendUserBean());
        return "需要获得kdream权限，才可以访问";
    }

    //mq测试
    @Resource
    RabbitTemplate rabbitTemplate;

    @PostMapping("/mq")
    public String mq(Integer a) {
        Map map=new HashMap();
        map.put("a",1);
        map.put("b",2);
        if(a==1){
            rabbitTemplate.convertAndSend("", "TestDirectRouting", "323232");
            return "1111";
        }
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", new Gson().toJson(map));
        return "security jwt";
    }
    @GetMapping("/captcha")
    public void getCaptcha(HttpServletResponse response) throws IOException {
        String captchaCode = CaptchaGeneratorUtils.getCaptchaCode(4);
        BufferedImage captchaImage = CaptchaGeneratorUtils.generateCaptchaLoginImage(captchaCode);

        response.setContentType("image/png");
        ImageIO.write(captchaImage, "png", response.getOutputStream());
    }
}



